package com.innovation.player;

import org.apache.log4j.Logger;

import com.google.protobuf.InvalidProtocolBufferException;
import com.innovation.cache.PlayerCache;
import com.innovation.cmd.BaseCmd;
import com.innovation.data.model.Player;
import com.innovation.exception.PatException;
import com.innovation.message.ServerMessageProto.ServerMessage;
import com.innovation.message.ServerMessageProto.ServerMessage.Builder;
import com.innovation.message.player.GetPlayerReqProto.GetPlayerReq;
import com.innovation.message.player.PlayerMsgProto.PlayerMsg;

/**
 * 获取玩家信息
 * @author Administrator
 *
 */
public class GetPlayerCmd extends BaseCmd {
	private static final Logger logger = Logger.getLogger(GetPlayerCmd.class);
	
	@Override
	protected void done(ServerMessage request, Builder response) throws PatException {
		try {
			GetPlayerReq req = GetPlayerReq.parseFrom(request.getData());
			int playerId = req.getPlayerId();
			int data = req.getData();
			Player player = PlayerCache.INSTANCE.getPlayer(playerId, data);
			
			PlayerMsg.Builder body = PlayerMsg.newBuilder();
			player.write2Proto(body);
			
			response.setData(body.build().toByteString());
		} catch (InvalidProtocolBufferException e) {
			logger.error("parse GetPlayerReq error.", e);
		}
	}

}
